package com.wufun.overseas.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.wufun.overseas.R;
import com.wufun.overseas.main.activity.HandShankNoActivity;
import com.wufun.overseas.main.activity.HandShankYesActivity;
import com.wufun.overseas.models.bean.BannerBean;
import com.wufun.overseas.models.bean.HandShankAdBean;
import com.wufun.overseas.models.bean.IntentDateBean;
import com.wufun.overseas.utils.Dip2SpUtil;
import com.wufun.overseas.utils.IntentUtil;
import com.wufun.overseas.utils.MyImageLoader;

/**
 * 手柄广告位View
 * <p>
 * Created by huzy on 2017/3/17.
 */

public class HandShankAdView extends LinearLayout implements LoadingLayout.RefreshCallBackListener {

    public HandShankAdView(Context context) {
        super(context);
        initView();
    }

    public HandShankAdView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public HandShankAdView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    protected LoadingLayout mLoadingLayout;
    private LinearLayout ll_fast_entrance;

    private void initView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.banner_handshank_top, this);
        mLoadingLayout = (LoadingLayout) view.findViewById(R.id.loadingLayout);
        mLoadingLayout.setRefreshCallBackListener(this);
        ll_fast_entrance = (LinearLayout) findViewById(R.id.ll_fast_entrance);
    }

    private IntentDateBean createnIntent(BannerBean bean) {
        return bean.getIntentDataBean();
    }

    public void updateViewByData(final HandShankAdBean data) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                final Context    context   = getContext();
                SimpleDraweeView iv_banner = (SimpleDraweeView) findViewById(R.id.iv_banner);
                final int        marginTop = Dip2SpUtil.dip2px(context, 8);
                if (null != data.getTop_banner_ad() && data.getTop_banner_ad().size() > 0) {
                    final BannerBean bannerBean = data.getTop_banner_ad().get(0);
                    if (bannerBean.getAd_switch() == 1) {
                        //开启
                        iv_banner.setVisibility(View.VISIBLE);
                        MyImageLoader.load(iv_banner, R.drawable.banner_normal_icon, bannerBean.getPic_remote());
                        iv_banner.setTag(bannerBean);
                        iv_banner.setOnClickListener(mOnClickListener);
                    } else {
                        //关闭
                        iv_banner.setVisibility(View.GONE);
                    }
                } else {
                    iv_banner.setVisibility(View.GONE);
                }
//                //FIXME:下面是测试地方,如果有必要，可以直接删除下面注释的地方
//                List<BannerBean> temp = new ArrayList<BannerBean>();
//                for (int i = 0; i < 5; i++) {
//                    BannerBean bannerBean = new BannerBean();
//                    bannerBean.setAd_switch(1);
//                    bannerBean.setSub_title("这是测试");
//                    temp.add(bannerBean);
//                }
//                data.getFast_entrance_ad().clear();
//                data.getFast_entrance_ad().addAll(temp);

                if (null != data.getFast_entrance_ad() && data.getFast_entrance_ad().size() > 0) {
                    int length = data.getFast_entrance_ad().size();

                    final int iv_size = Dip2SpUtil.dip2px(context, 48);
                    final int tv_size = Dip2SpUtil.dip2px(context,7);

                    for (int i = 0; i < length && i < 5; i++) {
                        BannerBean bean = data.getFast_entrance_ad().get(i);
                        if (bean.getAd_switch() == 0) {//==0 关闭 ==1开启
                            //如果是关闭的话，继续循环
                            continue;
                        }
                        LinearLayout child = new LinearLayout(context);
                        child.setLayoutParams(new LayoutParams(0, LayoutParams.MATCH_PARENT, 1.0f));
                        child.setGravity(Gravity.CENTER);
                        child.setOrientation(LinearLayout.VERTICAL);

                        SimpleDraweeView imageView = new SimpleDraweeView(context);
                        imageView.setLayoutParams(new LayoutParams(iv_size, iv_size));
                        imageView.setTag(bean);
                        imageView.setOnClickListener(mOnClickListener);
                        MyImageLoader.load(imageView, R.drawable.main_normal_icon, bean.getPic_remote());

                        TextView textView = new TextView(context);
                        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                        params.topMargin = marginTop;
                        textView.setLayoutParams(params);
                        textView.setTextSize(tv_size);
                        textView.setTextColor(Color.parseColor("#585858"));
                        textView.setText(bean.getSub_title());

                        child.addView(imageView);
                        child.addView(textView);
                        ll_fast_entrance.addView(child);
                    }
                } else {
                    ll_fast_entrance.setVisibility(View.GONE);
                }
                hideLoadingLayout();

                final int fastEntranceCount = ll_fast_entrance.getChildCount();
                if (iv_banner.getVisibility() == View.GONE) {
                    if (fastEntranceCount <= 0) {
                        hideView();
                    } else {
                        LayoutParams params = (LayoutParams) ll_fast_entrance.getLayoutParams();
                        params.topMargin = marginTop;
                        ll_fast_entrance.setLayoutParams(params);
                        //设置整体的高度
                        final int parentHeight = Dip2SpUtil.dip2px(context, 130);
                        LayoutParams parentParams = (LayoutParams) getLayoutParams();
                        parentParams.height = parentHeight;
                        setLayoutParams(parentParams);
                    }
                } else {
                    if (fastEntranceCount <= 0) {
                        //设置整体的高度
                        setPadding(0, 0, 0, marginTop);
                        final int parentHeight = Dip2SpUtil.dip2px(context, 130);
                        LayoutParams parentParams = (LayoutParams) getLayoutParams();
                        parentParams.height = parentHeight;
                        setLayoutParams(parentParams);
                        ll_fast_entrance.setVisibility(View.GONE);
                    }
                }
            }
        });
    }

    private final OnClickListener mOnClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            BannerBean bean = (BannerBean) v.getTag();
            if (null == bean) return;
            IntentUtil.getInstance().intentActivity(getContext(), createnIntent(bean));
        }
    };

    public void showNoNetWork() {
        hideView();
    }

    public void showDefaultView() {
        hideView();
    }

    @Override
    public void callBack() {
        showLoadingLayout();
        Activity activity = (Activity) getContext();
        if (activity instanceof HandShankYesActivity) {
            HandShankYesActivity yesActivity = (HandShankYesActivity) activity;
            yesActivity.loadAdData();
        } else if (activity instanceof HandShankNoActivity) {
            HandShankNoActivity noActivity = (HandShankNoActivity) activity;
            noActivity.loadAdData();
        }
    }

    //显示加载中
    protected void showLoadingLayout() {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                if (mLoadingLayout != null) {
                    mLoadingLayout.setVisibility(View.VISIBLE);
                    mLoadingLayout.showLoadingLayout();
                }
            }
        });
    }

    //显示加载失败页面
    protected void showFailLayout() {
        new Handler(Looper.getMainLooper()).post(new Runnable() {

            @Override
            public void run() {
                if (mLoadingLayout != null) {
                    mLoadingLayout.setVisibility(View.VISIBLE);
                    mLoadingLayout.showFailLayout();
                }
            }
        });
    }

    private void hideView() {
        new Handler(Looper.getMainLooper()).post(new Runnable() {

            @Override
            public void run() {
                if (mLoadingLayout != null) {
                    mLoadingLayout.hideLoadingLayout();
                    mLoadingLayout.setVisibility(View.GONE);
                    setVisibility(View.GONE);
                }
            }
        });
    }

    //隐藏加载页面
    protected void hideLoadingLayout() {
        new Handler(Looper.getMainLooper()).post(new Runnable() {

            @Override
            public void run() {
                if (mLoadingLayout != null) {
                    mLoadingLayout.hideLoadingLayout();
                    mLoadingLayout.setVisibility(View.GONE);
                }
            }
        });
    }
}
